﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.AspNet.Membership.OpenAuth;
using PaymentBLL;
using WebTriple7.Class;

namespace WebTriple7.UserControl
{
    public partial class ucLoginProvider : System.Web.UI.UserControl
    {
        public double Au = 0;
        public double Ag= 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Utils.UserUtils.IsLogin)
            {
                lblDisplayAcc.Text = Utils.UserUtils.FullName;

                var ctr = new CtrAccount();
                var info = ctr.SelectAccountInfo(Utils.UserUtils.AccountId);
                Au = info.Au;
                Ag = info.Ag;
                return;
            }
            else
            {
                lblDisplayAcc.Text = "";
                Au = 0;
                Ag = 0;
            }           



            Page.PreRenderComplete += Page_PreRenderComplete;


            if (IsPostBack)
            {
                var provider = Request.Form["provider"];
                if (provider == null)
                {
                    return;
                }

                var redirectUrl = "~/Portal/LoginExternal.aspx";
                if (!String.IsNullOrEmpty(ReturnUrl))
                {
                    var resolvedReturnUrl = ResolveUrl(ReturnUrl);
                    redirectUrl += "?ReturnUrl=" + HttpUtility.UrlEncode(resolvedReturnUrl);
                }

                OpenAuth.RequestAuthentication(provider, redirectUrl);
            }
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            AccountSession.Logout();
            Response.Redirect("~/Portal/Default.aspx");
        }
        protected void Page_PreRenderComplete(object sender, EventArgs e)
        {
            providersList.DataSource = OpenAuth.AuthenticationClients.GetAll();
            providersList.DataBind();
        }

        protected T Item<T>() where T : class
        {
            return Page.GetDataItem() as T ?? default(T);
        }


        public string ReturnUrl { get; set; }
    }
}